package com.xpgk.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import com.oreilly.servlet.multipart.ExceededSizeException;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.ddc.entity.Picture;
import com.xpgk.tools.PathFileKit;

import sun.misc.BASE64Encoder;

public class UploadPhotoUtil {
	
	
	public  static RenderBean uploadPhoto(UploadFile file,String BUSINESSIDS,String VEHICLEIDS,String PictureType,String pictureName) {
		RenderBean result = new RenderBean();
		try {
			String fileName = file.getFileName();
			File uploadFile = file.getFile();
			String extension = fileName.substring(fileName.lastIndexOf("."));
			if (".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension) || "jpeg".equals(extension) || "bmp".equals(extension)) {
				// 使用缩略图
//					fileName = new ImageKit().thumbnailImage(uploadFile, 150, 150, "thumb_", false);
//					File source = new File(fileName);
				
				String savePath = PathKit.getWebRootPath() + File.separator+"carPhoto"+File.separator+BUSINESSIDS;
				//String savePath = "D:/ddc/carPhoto/"+BUSINESSIDS;
				String saveFileName = PictureType +  extension;

				String BASE64 = "";
				try {
					FileInputStream inputFile = new FileInputStream(uploadFile);
					byte[] buffer = new byte[(int) uploadFile.length()];
					inputFile.read(buffer);
					inputFile.close();
					BASE64 = new BASE64Encoder().encode(buffer);
					File p = new File(savePath);
					if (!p.exists())
						p.mkdirs();
						PathFileKit.copyFile(uploadFile, new File(savePath + File.separator + saveFileName));
						result.setCode("1");
					} catch (IOException e1) {
						e1.printStackTrace();
						result.setCode("0");
						result.setMsg("文件写入服务器出现错误，请稍后再上传");
					}
//					source.delete();
				String src = "carPhoto/" +BUSINESSIDS+"/" + saveFileName;
				//result.setCode("src", src); // 相对地址，显示图片用
//				  int userID = UserService.getUser(userName).getInt("id");
				//Db.update("update DDC_VEHICLE set PHOTO=? where IDS=?", src, IDS);//旧方法
				SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Picture picture = new Picture();
				picture.set(Picture.column_VEHICLEIDS, VEHICLEIDS);
				picture.set(Picture.column_TYPE, PictureType);
				picture.set(Picture.column_PICTURENAME, pictureName);
				picture.set(Picture.column_PICTUREPATH, src);
				picture.set(Picture.column_CREATEDATE, sdf.format(new Date()));
				picture.set(Picture.column_BUSINESSIDS, BUSINESSIDS);
				picture.set(Picture.column_PICSTR, BASE64);
				picture.save();
				Db.update("update DDC_BUSINESS set " +pictureName+ " = ? where IDS = ?", saveFileName, BUSINESSIDS);
			} else {
				result.setCode("0");
				result.setMsg("只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
			}
			uploadFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof ExceededSizeException){
				result.setCode("0");
				result.setMsg("最大上传2M的图片");
			}else{
				result.setCode("0");
				result.setMsg(e.toString());
			}
		}
		return result;
	}

}
